#include <iostream>
#include <string>
#include <string.h>
#include <cerrno>
#include <unistd.h>
#include "Thread.hpp"

// 网路编程常用套接字
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
// 声明区
class ThreadData;

void Usage(const std::string &s);
void *ReceiveMessage(ThreadData &td, bool *result);
void *SendMessage(ThreadData &td, bool *result);
bool VisitServer(std::string &server_ip, uint16_t &server_port, int *count);

#define RETRY_COUNT 5

int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        Usage(argv[0]);
        return 1;
    }
    std::string ip = argv[1];
    uint16_t port = std::stoi(argv[2]);

    int count = 1;
    while (count <= RETRY_COUNT)
    {
        // 客户端访问服务
        bool result = VisitServer(ip, port, &count); // 服务的返回值，true正常退出

        if (result == true)
        {
            break;
        }
        else // false需要重连
        {
            sleep(1);
            std::cout << "server offline, retrying..., count: " << count << std::endl;
            count++;
        }
    }
    // 重连次数限制
    if (count >= RETRY_COUNT)
    {
        std::cout << "server offline, client exit finally" << std::endl;
    }
}

// 实现区

class ThreadData
{
public:
    ThreadData(const struct sockaddr_in &addr, const int &sock_fd)
        : _addr(addr), _sock_fd(sock_fd) {}
    ~ThreadData() {}
    const struct sockaddr_in &Addr() const { return _addr; }
    const int &SockFd() const { return _sock_fd; }

private:
    struct sockaddr_in _addr;
    int _sock_fd;
};

void Usage(const std::string &s)
{
    std::cout << "warning: illegal usage!" << std::endl;
    std::cout << "please input as this: " << s.c_str() << " server_ip server_port" << std::endl;
}

// 访问服务器
bool VisitServer(std::string &server_ip, uint16_t &server_port, int *count)
{

    // 创建套接字
    int sock_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (sock_fd < 0)
    {
        std::cout << "falied to run socket" << std::endl;
        return false;
    }

    // 绑定服务器
    struct sockaddr_in server;
    server.sin_family = AF_INET;
    server.sin_port = htons(server_port);
    inet_pton(AF_INET, server_ip.c_str(), &server.sin_addr)
    // server.sin_addr.s_addr = inet_addr(server_ip.c_str());

    // TCP链接服务器
    if (connect(sock_fd, (struct sockaddr *)&server, sizeof(server)) < 0)
    {
        std::cout << "failed to run connect" << std::endl;
        return false;
    }

    // 输出型参数
    *count = 1; // 重置更新数目

    char buffer[1024];

    ssize_t n = read(sock_fd, buffer, sizeof(buffer));
    if (n > 0)
    {
        buffer[n] = 0;
        std::cout << "server message: "<<buffer << std::endl;
    }
    else if (n == 0)
    {
        std::cout << "server quit" << std::endl;
        return false;
    }

    std::string message;
    std::cout << "please enter# ";
    std::getline(std::cin, message);

    if (message == "quit") // 发送退出信息给服务器
    {
        return true;
    }
    ssize_t m = write(sock_fd, message.c_str(), message.size());
    if (m > 0)
    {
        char buffer[1024]
    }
    else
    {
    }

    close(sock_fd);
    return false;
}